from locust import task, FastHttpUser, between, TaskSet, User
import json, queue


# class MyUser(FastHttpUser):
#     wait_time = between(1, 2)
#
#     @task
#     def index(self):    #host：https://***********.com
#         # user_data_queue = queue.Queue()
#         # for index in range(10001,10051):
#         #     user_data_queue.put_nowait(index)
#         # try:
#         #     data = user_data_queue.get()
#         # except queue.Empty:
#         #     print('数据已用尽，测试结束。')
#         #     exit(0)
#         # payload = {
#         #     "vcode": "135246",
#         #     "orgNumber": 1998,
#         #     "phone": f"{data}"
#         # }
#         # self.client.post('/j***/member/login', json=payload)
#         # print(data)
#         # user_data_queue.put_nowait(data)
#
#         # payload = {
#         #     "vcode": "1****6",
#         #     "orgNumber": 1998,
#         #     "phone": "44591"
#         # }
#         # res = self.client.post('/****ber/login', json=payload)
#         # token = json.loads(res.content.decode('utf8'))['data']['extInfo']['x-token']
#
#         # payload2 = {
#         #     "orderId":11144
#         # }
#         # header = {
#         #     'x-token': token
#         # }
#         # self.client.post('/*******/queryExpressByOrderId', headers=header, json=payload2)


class UserBehavior(TaskSet):

    def __init__(self, parent: "User"):
        super().__init__(parent)
        self.root_index = None

    def on_start(self):
        # print('taskset start')
        self.root_index = 0

    def on_stop(self):
        print('taskset end')

    @task
    def get_root(self):
        # print("get_root task")
        # print("root index : " + str(self.root_index))
        self.root_index += 1

        if not self.parent.queue_list.empty():
            data = self.parent.queue_list.get()
            # print("queue data:{}".format(data))
            # response = self.client.get('', name='get_root')
            payload = {
                "vcode": "1****46",
                "orgNumber": 1998,
                "phone": f"{data}"
            }
            print(f"当前phone账号。。。。。。。。。： {data}")
            res = self.client.post('/*******r/login', json=payload)
            token = json.loads(res.content.decode('utf8'))['data']['extInfo']['x-token']

            # #  积分购买商品
            # payload2 = {
            #     "sending": 1*,
            #     "type": "H**W",
            #     "mateIds": "102**42",
            #     "goodsSpec": "6*29",
            #     "deliveryAddr": "",
            #     "num": 1,
            #     "memberAccount": f"{data}",
            #     "proId": "网*云",
            #     "description": "兑换1*测试商品"
            #     # "sending": 1*, "type": "HW*", "mateId*s": "1024*7", "good*sSpec": "637*", "deliveryAddr": "", "num": 1,
            #     # "memberAc*count": "", "proId": "LB*4", "descrip*tion": "兑换*列表4"
            # }
            # header = {
            #     'X-Token': token
            # }
            # self.client.post('/*******exchange', headers=header, json=payload2)

            # # 点击抽奖
            # payload3 = {
            #     "raffleId": 3,
            #     "ids": [
            #         76,
            #         78
            #     ],
            #     "sending": 1
            # }
            # header3 = {
            #     'X-Token': token
            # }
            # self.client.post('/jf/********e/start', headers=header3, json=payload3)

            #加积分
            payload4 = {
                "score": 10,
                "description": "压测加积分",
                "phone": 64727,
                "scoreType": "YACE",
                "orgNum": 1998
            }
            header4 = {
                'X-Token': token
            }
            self.client.post('/j******r/addScore', headers=header4, json=payload4)



        else:
            print("no data exist")
            exit(0)

        # if not response.ok:
        #     print(response.text)
        #     response.failure('Got wrong response')


class TestLocust(FastHttpUser):
    # If no wait_time is specified, the next task will be executed as soon as one finishes.
    wait_time = between(5, 5)

    def on_start(self):
        print('locust user start')

    def on_stop(self):
        print('locust user stop')

    tasks = [UserBehavior]
    host = "https://********.com"

    queue_list = queue.Queue()
    # for i in range(10001, 10051):
    #     queue_list.put_nowait(i)

    for i in range(10001, 10101):
        queue_list.put_nowait(i)
